package com.zzy.module.admin.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.zzy.common.domain.ParameterErrorBack;
import com.zzy.common.domain.RI;
import com.zzy.common.domain.RSD;
import com.zzy.common.entity.db.AdminDaoEntity;
import com.zzy.common.exception.BotException;
import com.zzy.common.util.LoginUtils;
import com.zzy.common.util.ValidatorUtils;
import com.zzy.module.admin.entity.UpdatePwd;
import com.zzy.module.admin.entity.User;
import com.zzy.module.admin.entity.UserLogin;
import com.zzy.module.admin.mapper.AdminMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author zhuZhaoYang
 * @date 2021/12/4 18:57
 */
@RestController
@Validated
@RequestMapping("/")
public class LoginController {

    private Producer producer;
    private AdminMapper adminMapper;

    @Autowired
    public void setProducer(Producer producer) {
        this.producer = producer;
    }

    @Autowired
    public void setAdminService(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }


    @ApiOperation(value = "验证码")
    @GetMapping(value = "/captcha", produces = "image/jpeg")
    public void captcha(HttpServletResponse response) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        String text = producer.createText();
        System.out.println("验证码内容：" + text);
        BufferedImage image = producer.createImage(text);
        LoginUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        out.flush();
    }

    @ApiOperation("登录")
    @Transactional(rollbackFor = {Exception.class})
    @PostMapping("/login")
    public RI login(@RequestBody UserLogin user) {
        ValidatorUtils.validateEntity(user);
//        String rightCode = LoginUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
//        if (!user.getCode().equals(rightCode)) {
//            return new ParameterErrorBack("验证码错误");
//        }
        LoginUtils.login(user.getUsername(), user.getPassword());
        AdminDaoEntity curUser = LoginUtils.getLoginUser();
        String identity = adminMapper.returnIdentity(curUser.getActorType());
        User returnData = new User(user.getUsername(), identity);
        return new RSD(returnData);
    }

    @ApiOperation(value = "登出")
    @GetMapping("/logout")
    public RI logOut() {
        LoginUtils.logout();
        return new RI(200, "退出登录成功");
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/modPassword")
    public RI updatePassword(@RequestBody UpdatePwd pwd) {
        ValidatorUtils.validateEntity(pwd);
        if (!LoginUtils.getLoginUser().getPassword().equals(LoginUtils.md5(pwd.getOldPwd(), LoginUtils.getLoginUser().getUsername()))) {
            throw new BotException(500, "旧密码输入不正确");
        }
        AdminDaoEntity user = LoginUtils.getLoginUser();
        String newPassword = LoginUtils.md5(pwd.getNewPwd(), user.getUsername());
        adminMapper.myUpdate(newPassword, user.getId());
        return new RI(200, "修改密码成功");
    }

    @ApiOperation("未登录")
    @GetMapping("/notLogin")
    public RI notLogin() {
        return new ParameterErrorBack("请先登录");
    }

}
